package com.yyuap.enterprise.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import com.alibaba.fastjson.JSONArray;
import com.yonyou.uap.um.context.util.UmContextUtil;
import com.yonyou.uap.um.controller.AbstractUMController;
import com.yyuap.enterprise.impl.UserImpl;
import com.yyuap.enterprise.itf.IUser;
import com.yyuap.enterprise.vo.UserVO;

public class EnterpriseController extends AbstractUMController {
	public String loadData(String arg0) throws Exception {
		JSONObject jsonObj = new JSONObject(arg0);
		Map jsonmap = UmContextUtil.transJsonToMap(jsonObj);
		String condition = (String) jsonmap.get("condition");

		JSONObject resultJson = new JSONObject();
		JSONArray stuArray = new JSONArray();
		String flag = "";
		String des = "";

		try {
			IUser iuser = new UserImpl();
			List<UserVO> users = iuser.findUser(condition);
			for (int i = 0; i < users.size(); i++) {
				JSONObject userObj = new JSONObject();
				UserVO user = users.get(i);
				userObj.put("id", user.getId());
				userObj.put("stuID", user.getStuid());
				userObj.put("name", user.getName());
				userObj.put("sex", user.getSex());
				userObj.put("age", user.getAge());
				userObj.put("score", user.getScore());
				stuArray.add(userObj);
			}
			resultJson.put("stuList", stuArray);
			flag = "success";
			des = "加载数据成功";

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			flag = "success";
			des = e.getMessage();
		} finally {
			resultJson.put("flag", flag);
			resultJson.put("des", des);

		}
		return resultJson.toString();
	}

	public String addData(String arg0) throws Exception {
		JSONObject jsonObj = new JSONObject(arg0);
		Map jsonmap = UmContextUtil.transJsonToMap(jsonObj);
		String userInfor = (String) jsonmap.get("userInfor");

		JSONObject userObj = new JSONObject(userInfor);
		Map usermap = UmContextUtil.transJsonToMap(userObj);
		String stuID = (String) usermap.get("stuID");
		String name = (String) usermap.get("name");
		String sex = (String) usermap.get("sex");
		int age = Integer.valueOf((String) usermap.get("age"));
		Float score = Float.parseFloat((String) usermap.get("score"));

		UserVO user = new UserVO();
		user.setStuid(stuID);
		user.setName(name);
		user.setSex(sex);
		user.setAge(age);
		user.setScore(score);

		//
		// Gson gson = new Gson();
		// gson.toJson(userObj, UserVO.class);
		JSONObject resultJson = new JSONObject();
		String flag = "";
		String des = "";

		try {
			IUser iuser = new UserImpl();
			String addStr = iuser.insertUser(user);
			flag = "success";
			des = "添加数据成功";
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			flag = "success";
			des = e.getMessage();
		} finally {
			resultJson.put("flag", flag);
			resultJson.put("des", des);

		}
		return resultJson.toString();
	}

	public String delData(String arg0) throws Exception {
		JSONObject jsonObj = new JSONObject(arg0);
		Map jsonmap = UmContextUtil.transJsonToMap(jsonObj);
		String id = (String) jsonmap.get("condition");

		JSONObject resultJson = new JSONObject();
		String flag = "";
		String des = "";
		try {
			IUser iuser = new UserImpl();
			UserVO user = new UserVO();
			user.setId(id);
			iuser.deleteUser(user);
			flag = "success";
			des = "删除数据成功";
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			flag = "success";
			des = e.getMessage();
		} finally {
			resultJson.put("flag", flag);
			resultJson.put("des", des);

		}
		return resultJson.toString();

	}

	public String updateData(String arg0) throws Exception {
		JSONObject jsonObj = new JSONObject(arg0);
		Map jsonmap = UmContextUtil.transJsonToMap(jsonObj);
		String id = (String) jsonmap.get("id");
		String userInfor = (String) jsonmap.get("userInfor");

		JSONObject userObj = new JSONObject(userInfor);
		Map usermap = UmContextUtil.transJsonToMap(userObj);
		String stuID = (String) usermap.get("stuID");
		String name = (String) usermap.get("name");
		String sex = (String) usermap.get("sex");
		int age = Integer.valueOf((String) usermap.get("age"));
		Float score = Float.parseFloat((String) usermap.get("score"));

		UserVO user = new UserVO();
		user.setId(id);
		user.setStuid(stuID);
		user.setName(name);
		user.setSex(sex);
		user.setAge(age);
		user.setScore(score);

		//
		// Gson gson = new Gson();
		// gson.toJson(userObj, UserVO.class);
		JSONObject resultJson = new JSONObject();
		String flag = "";
		String des = "";

		try {
			IUser iuser = new UserImpl();
			iuser.updateUser(user);
			flag = "success";
			des = "更新数据成功";
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			flag = "success";
			des = e.getMessage();
		} finally {
			resultJson.put("flag", flag);
			resultJson.put("des", des);

		}
		return resultJson.toString();
	}

	@Override
	public String load(String arg0) throws Exception {
		// TODO 自动生成的方法存根
		return null;

	}

	@Override
	public String save(String arg0) throws Exception {
		// TODO 自动生成的方法存根
		return null;
	}

}
